package com.lancer.dynamic.thread.pool.lancerenum;

import com.lancer.dynamic.thread.pool.config.ThreadPoolConfig;
import com.lancer.dynamic.thread.pool.utils.SystemUtils;

import java.util.concurrent.TimeUnit;

/**
 * 线程池预定的运行模式
 */
public enum RunModelEnum {
    // IO 密集型任务：核心线程数和最大线程数动态设置
    IO {
        @Override
        public ThreadPoolConfig getThreadPoolConfig() {
            return getThreadPoolConfig("io-pool");
        }

        @Override
        public ThreadPoolConfig getThreadPoolConfig(String poolName) {
            ThreadPoolConfig config = new ThreadPoolConfig();
            config.setPoolName(poolName);

            int physicalCoreCount = SystemUtils.getPhysicalCoreCount();

            // I/O 密集型：核心线程数 = CPU核心数 * 4，最大线程数 = CPU核心数 * 8
            config.setCorePoolSize(physicalCoreCount * 4);
            config.setMaxPoolSize(physicalCoreCount * 8);
            config.setKeepAliveTime(60);
            config.setUnit(TimeUnit.SECONDS);
            config.setQueueCapacity(5000);
            config.setRejectEnum(RejectEnum.DISCARD);
            return config;

        }
    },
    // CPU 密集型任务：核心线程数 = CPU核心数 + 1，最大线程数 = CPU核心数 * 2
    CPU {
        @Override
        public ThreadPoolConfig getThreadPoolConfig() {
            return getThreadPoolConfig("cpu-pool");
        }

        @Override
        public ThreadPoolConfig getThreadPoolConfig(String poolName) {
            ThreadPoolConfig config = new ThreadPoolConfig();
            config.setPoolName(poolName);

            int physicalCoreCount = SystemUtils.getPhysicalCoreCount();

            // CPU 密集型：核心线程数 = CPU核心数 + 1，最大线程数 = CPU核心数 * 2
            config.setCorePoolSize(physicalCoreCount + 1);
            config.setMaxPoolSize(physicalCoreCount * 2);
            config.setKeepAliveTime(60);
            config.setUnit(TimeUnit.SECONDS);
            config.setQueueCapacity(100);
            config.setRejectEnum(RejectEnum.DISCARD);
            return config;
        }
    },
    // 混合型任务：折中策略
    MIXED {
        @Override
        public ThreadPoolConfig getThreadPoolConfig() {
            return getThreadPoolConfig("mixed-pool");
        }

        @Override
        public ThreadPoolConfig getThreadPoolConfig(String poolName) {
            ThreadPoolConfig config = new ThreadPoolConfig();
            config.setPoolName(poolName);

            int physicalCoreCount = SystemUtils.getPhysicalCoreCount();

            // 混合型：核心线程数 = CPU核心数，最大线程数 = CPU核心数 * 2
            config.setCorePoolSize(physicalCoreCount);
            config.setMaxPoolSize(physicalCoreCount * 2);
            config.setKeepAliveTime(60);
            config.setUnit(TimeUnit.SECONDS);
            config.setQueueCapacity(1000);
            config.setRejectEnum(RejectEnum.DISCARD);
            return config;
        }
    };

    /**
     * 获取对应运行模式下的线程池配置
     *
     * @return ThreadPoolConfig 线程池配置对象
     */
    public abstract ThreadPoolConfig getThreadPoolConfig();

    public abstract ThreadPoolConfig getThreadPoolConfig(String poolName);

}
